<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
var echarts = require("echarts");
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import { get_book_click_top, get_book_detail } from "@/api/book";
const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
  },
  data() {
    return {
      chart: null,
      list_X: [],
      list_Y: [],
    };
  },

  async mounted() {
    await this.get_top();
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    //获取点击排名前十图书
    async get_top() {
      await get_book_click_top({
        page: 1,
        pageSize: 14,
      }).then(async (res) => {
        console.log("获取排行榜");
        console.log(res.data.data);
        for (let index = 0; index < res.data.data.records.length; index++) {
          //查询图书详情
          await get_book_detail({
            bookId: res.data.data.records[index].bookId,
          }).then((res) => {
            console.log(res.data.data);
            this.list_Y.push(res.data.data.title);
          });
        }

        this.list_X = res.data.data.records.map((item) => item.hits);
        this.list_Y = this.list_Y.map((item) => item);
        console.log(this.list_X);
        console.log(this.list_Y);
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.list_X, this.list_Y);
    },
    setOptions(data_x, data_y) {
      this.chart.setOption({
        title: {
          text: "图书点击 Top 10",
          textStyle: {
            fontSize: 15,
          },
          top:'-5px',
          left: "center",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
          textStyle: {
            color: "#fff",
          },
        },
        grid: {
          top: 10,
          left: "2%",
          right: "2%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "value",
            axisTick: {
              show: false,
            },
          },
        ],
        yAxis: [
          {
            axisLabel: {
              formatter: function (value) {
                let valueTxt = "";
                if (value.length > 3) {
                  valueTxt = value.substring(0, 4) + "...";
                } else {
                  valueTxt = value;
                }
                return valueTxt;
              },
            },

            type: "category",
            data: data_y,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        series: [
          {
            name: "pageA",
            type: "bar",
            stack: "vistors",
            barWidth: "60%",
            data: data_x,
            animationDuration,

            colorBy: "index",
          },
        ],
      });
    },
  },
};
</script>
